﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class SystemManage_ContainerModelAdd : BasePage
{
    ISyContainerModelHandler modelManage = new SyContainerModelHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!(strPermission.Contains("1016") || strPermission.Contains("1017")))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }
          
            dataBind();
        }
    }

    private void dataBind()
    {

        if (Request["PKID"] != null)
        {
            Sy_ContainerModelQuery query = modelManage.GetModel(int.Parse(Request["PKID"]));
            if (query != null)
            {
                //lblUpCode.Text = query.Code;
                // txtCode.Text = query.Code;
                txtModel.Text = query.Model;
                txtRemark.Value = query.Remark;
                //txtType.Text = query.Type;
                txtVolLowerLimit.Text = query.VolLowerLimit.ToString();
                txtVolUpperLimit.Text = query.VolUpperLimit.ToString();
                if (query.Status == 1)
                {
                    radYes.Checked = true;
                    radNo.Checked = false;
                }
                else
                {
                    radYes.Checked = false;
                    radNo.Checked = true;
                }

            }
            // txtCode.Visible = false;
            // lblUpCode.Visible = true;
        }
       
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Sy_ContainerModelQuery query = new Sy_ContainerModelQuery();
       
        if (Request["PKID"] != null)
        {
          //  txtCode.Visible = false;
          //  lblUpCode.Visible = true;

            Sy_ContainerModelQuery isQuery = modelManage.GetModel(int.Parse(Request["PKID"]));
            if (isQuery != null)
            {
                query = isQuery;

                query.Code = string.Empty;
                query.Model = txtModel.Text.Trim();
                query.Remark = txtRemark.Value.Trim();
                query.VolUpperLimit = decimal.Parse(txtVolUpperLimit.Text.Trim());
                query.VolLowerLimit = decimal.Parse(txtVolLowerLimit.Text.Trim());
                if (radYes.Checked)
                {
                    query.Status = 1;
                }
                else
                {
                    query.Status = 0;
                }

                query.Type = string.Empty;
                query.LastModDate = DateTime.Now;
                if (Session["UserID"] != null)
                {
                    query.LastModUser = int.Parse(Session["UserID"].ToString());
                }

                else
                {
                    query.LastModUser = 0;
                }
            }

            if (modelManage.ModelUpdate(query))
            {
                Jscript.AlertAndRedirect(Resources.Messages.UpdateSuccess, "ContainerModelList.aspx", this.Page);
            }
            else
            {
                Jscript.Alert(Resources.Messages.UpdateFailed, this.Page);
            }
        }
        else
        {
          //  txtCode.Visible = true;
           // lblUpCode.Visible = false;

            DataTable dtModel = modelManage.GetModelList(txtModel.Text.Trim());
            if (dtModel != null && dtModel.Rows.Count > 0)
            {
                Jscript.Alert(Resources.Messages.NameExists, this.Page);
                return;
            }
            else
            {
                query.Code = string.Empty;
                query.Model = txtModel.Text.Trim();
                query.Remark = txtRemark.Value.Trim();
                query.VolUpperLimit = decimal.Parse(txtVolUpperLimit.Text.Trim());
                query.VolLowerLimit = decimal.Parse(txtVolLowerLimit.Text.Trim());
                if (radYes.Checked)
                {
                    query.Status = 1;
                }
                else
                {
                    query.Status = 0;
                }

                query.Type = string.Empty;
                query.LastModDate = DateTime.Now;
                if (Session["UserID"] != null)
                {
                    query.LastModUser = int.Parse(Session["UserID"].ToString());
                }

                else
                {
                    query.LastModUser = 0;
                }

                if (modelManage.ModelInsert(query))
                {

                    Jscript.AlertAndRedirect(Resources.Messages.AddSuccess, "ContainerModelList.aspx", this.Page);
                }
                else
                {
                    Jscript.Alert(Resources.Messages.AddFailed, this.Page);
                }
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("ContainerModelList.aspx");
    }
}
